/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Handlers;

import apiAccess.APIAccess;
import apiAccess.APIEXception;
import apiAccess.Product;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.SAXException;

/**
 *
 * @author Kevin
 */
public class ProductHandler {
    Product p;
    int pos;
    List<Integer> idList;

    public ProductHandler(int n, List<Integer> idList){
        this.pos=n-1;
        this.idList=idList;
    }

   public Product next() throws SAXException, IOException, APIEXception{
       if(pos==this.idList.size()){
           return null;
       }
       pos++;
       p=APIAccess.getProduct(this.idList.get(pos-1));
       return p;
   }

   public Product back() throws SAXException, IOException, APIEXception{
       if(pos==1){
           return null;
       }
       pos--;
       p=APIAccess.getProduct(this.idList.get(pos-1));
       return p;
   }

   public boolean activoNext(){
       if(pos==this.idList.size()){
           return false;
       }
       return true;

   }

    public boolean activoBack(){
       if(pos==1){
           return false;
       }
       return true;

   }
    public int cantProductos() {
        if(hayProducto())
            return idList.size();
        return 0;
    }

    public boolean hayProducto(){
        return p!=null;
    }

    public int pages() {
        return idList.size();
    }

    public String pagesFoot(){
        return pos+"/"+pages();
    }

    public List<String> getProductNameList() throws SAXException, IOException, APIEXception{
        List<String> nameList= new ArrayList<String>();
        for(Integer id:idList){
            nameList.add(APIAccess.getProductName(id));
        }
        return nameList;
    }
}
